<template>
    <div>
        <div class="node" style="margin: 0px 0px 5px 5px">
            <span>节点: </span>
            <Select  v-model="node" style="width:200px;margin-right: 10px" @on-change="changeNode">
                <template v-for="node in node_list" >
                    <Option :key="node" :value="node" >{{ node }} </Option>
                </template>
            </Select>
            <Button @click="btn_refresh" type="primary">刷新</Button> 
        </div>
        <i-table border stripe  :columns="format" :data="show_list"  height="720">
            <template slot-scope="{ row, index }" slot="action">
                <Button type="error" style="margin-bottom: 5px "  @click="del_node_pod(index)">删除</Button>
            </template>
        </i-table >
        <div style="text-align: center;margin-top: 10px;">
            <Page ref="page" :total="total" :page-size="pageSize"  @on-change="changePage" @on-page-size-change="changePageSize" show-total show-sizer/>
        </div>
    </div>

</template>
<script>

import {k8s_namespace_post,k8s_cluster_post} from '@/api/http'
import { delete_pod,get_pod_list_by_node,get_node_name_list} from  '@/api'
import { delete_namespace_resource } from '@/common/util.js'


export default {
    data() {
        return {
            format: [
                {
                    title: '名字',key: 'name',
                    render: (h,params) => {
                        return h('div',[
                            h('a',{
                                style: {
                                    color: 'blue'
                                },
                                on: {
                                    click: ()=>{
                                        this.handleLink(params);
                                    }
                                }
                            },params.row.name)
                        ])
                    }
                },
                {
                    title: '命名空间',key: 'namespace'
                },
                {
                    title: '状态',key: 'status',
                    render: (h,params) => {
                        let status = params.row.status
                        let color = ''
                        switch(status){
                            case 'Running':
                                color = '#228B22'
                                break
                            case 'Pending':
                                color = '#ff9900'
                                break
                            case 'Succeeded':
                                color = '#2b85e4'
                                break
                            case 'Failed':
                                color = '#ed4014'
                                break
                            case 'Unknown':
                                color = '#2db7f5'
                                break
                            default:
                                color = '#2db7f5'
                        }
                        return h('span',{style: {color: color,}},status)
                    }
                },
                {
                    title: 'CPU使用',key:'pod_usage',sortable: true,
                    sortMethod:function(a,b,type) {
                        if (type == 'asc') {
                            return  a['cpu']< b['cpu'] ? -1 : 1

                        } else {
                            return  a['cpu']> b['cpu'] ? -1 : 1
                        }
                    },
                    render: (h,params) =>{
                        let pod_usage = params.row.pod_usage
                        let cpu_usage = pod_usage.cpu
                        let value=''
                        if(cpu_usage < 1000){
                            value = cpu_usage + "m"
                        }else{
                            value = (cpu_usage/1000).toFixed(1)+"c"
                        }
                        return h('p',value)
                    }
                },
                {
                    title: '内存使用',key:'pod_usage',sortable: true,
                    sortMethod:function(a,b,type) {
                        if (type == 'asc') {
                            return  a['memory']< b['memory'] ? -1 : 1

                        } else {
                            return  a['memory']> b['memory'] ? -1 : 1
                        }
                    },
                    render: (h,params) =>{
                        let pod_usage = params.row.pod_usage
                        let memory_usage = pod_usage.memory
                        let value=''
                        if(memory_usage < 1024){
                            value = memory_usage + "M"
                        }else{
                            value = (memory_usage/1024).toFixed(1)+"G"
                        }
                        return h('p',value)
                    }   
                },
                {
                    title: 'Pod最低需求',key: 'requests',
                    render: (h, params) => {
                        let resources = params.row.resources
                        let requests = resources.requests
                        // let req_cpu = parseInt(requests['cpu'])
                        // let req_memory = parseInt(requests['memory'])
                        let req_cpu = requests['cpu']
                        let req_memory = requests['memory']
                        let cpu=''
                        if(req_cpu==0){
                            cpu = '无'
                        }else if(req_cpu < 1000){
                            cpu = req_cpu + "m"
                        }else{
                            cpu = (req_cpu/1000).toFixed(1)+"c"
                        }
                        let memory=''
                        if(req_memory==0){
                            memory = '无'
                        }else if(req_memory < 1024){
                            memory = req_memory + "M"
                        }else{
                            memory = (req_memory/1024).toFixed(1)+"G"
                        }
                        let value = "cpu:"+cpu+"\n"+"memory:"+memory
                        return h('pre',value)

                    }

                },
                {
                    title: 'Pod最高要求',key: 'limits',
                    render: (h, params) => {
                        let resources = params.row.resources
                        let limits = resources.limits
                        // let limit_cpu = parseInt(limits['cpu'])
                        // let limit_memory = parseInt(limits['memory'])
                        let limit_cpu = limits['cpu']
                        let limit_memory = limits['memory']
                        let cpu=''
                        if(limit_cpu==0){
                            cpu = '无'
                        }else if(limit_cpu < 1000){
                            cpu = limit_cpu + "m"
                        }else{
                            cpu = (limit_cpu/1000).toFixed(1)+"c"
                        }
                        let memory=''
                        if(limit_memory == 0){
                            memory = '无'
                        }else if(limit_memory < 1024){
                            memory = limit_memory + "M"
                        }else{
                            memory = (limit_memory/1024).toFixed(1)+"G"
                        }
                        let value = "cpu:"+cpu+"\n"+"memory:"+memory
                        return h('pre',value)

                    }
                },
                // {
                //     title: '节点',key: 'node',
                // },
                {
                    title: 'pod_ip',key: 'pod_ip',
                },

                // {
                //     title: '镜像',key: 'image',
                // },
                {
                    title: '操作',slot: 'action',width: 100,align: 'center'
                }
            ],
            node_list: [],
            node: '',
            // total_list: [],
            show_list: [],
            total: 0,
            pageSize: 10,
            page: 1,
        }
    },
    methods: {
        btn_refresh(){
            this.refresh()
        },
        // 链接点击事件，进入详情页
        handleLink(params){
            // let namespace = params.row.namespace
            // this.$router.push({name: 'podDetail',query: {name: params.row.name,namespace: namespace}})
            let namespace = params.row.namespace
            const { href } = this.$router.resolve({
                name: 'podDetail'
            });
            window.open(href+"?name="+params.row.name+"&namespace="+namespace, '_blank');
        },
        changePage(page) {
            this.page=page
            this.refresh()
        },
        // 每页条数变更
        changePageSize(pageSize){
            console.log(pageSize)
            this.pageSize = pageSize
            this.refresh()
        },
        del_pod(namespace,pod_name){
            let name = pod_name
            let result = confirm("确定要删除"+name+"吗?")
            if(result == false) return 
            delete_namespace_resource(namespace,name,delete_pod)
            this.refresh()
        },

        del_node_pod(index){
            let pod_name = this.show_list[index].name
            let namespace = this.show_list[index].namespace
            if(pod_name && namespace){
                this.del_pod(namespace,pod_name)
            }
        },

        changeNode() {
            let node = this.node
            localStorage.setItem("podStatNode",node)
            this.refresh()
        },

        // 获取节点名称列表
        get_node_list(){
            k8s_cluster_post(get_node_name_list).then(response => { 
                let node_list = response.data
                if(node_list){
                    this.node_list = node_list
                    this.node = this.node_list[0]
                }
            })
        },
        // 刷新按node展示的pod列表
        refresh(){
            let node = localStorage.getItem("podStatNode")
            // bug 切换集群，节点选中丢失，已判断
            if(!node ||this.node_list.indexOf(node) == -1 ){
                node = this.node_list[0]
            }else{
                // 重设选中项
                this.node = node
            }
            let data = {"node":this.node}
            let params = {
                "pageSize":this.pageSize,
                "page":this.page,
            }
            // 小问题 把命名空间也传给后台了
            k8s_namespace_post(get_pod_list_by_node,data,{},30000,params).then( (response) => {
                let data = response.data
                this.total  = data.total
                this.show_list = data.pod_list
            })
        },

    },
    mounted: function(){
        this.get_node_list();
        setTimeout(() =>{
            this.refresh();
        },300); 
    },
}
</script>
<style>
    pre{
        white-space: pre-wrap!important;
        word-wrap: break-word!important;
        *white-space:normal!important;
    }
</style>